package com.kh.rationaldrugreview.controller;

import com.kh.rationaldrugreview.entity.Prescription;
import com.kh.rationaldrugreview.service.PrescriptionService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 处方查询控制器
 * 提供处方信息的查询接口，用于支持审查系统的数据展示
 * 
 * @author Claude Code
 * @version 1.0
 */
@RestController
@RequestMapping("/api/prescriptions")
@RequiredArgsConstructor
public class PrescriptionController {
    
    private final PrescriptionService prescriptionService;
    
    /**
     * 获取所有处方列表
     * 按处方日期倒序排列
     * 
     * @return 处方列表
     */
    @GetMapping
    public ResponseEntity<List<Prescription>> getAllPrescriptions() {
        List<Prescription> prescriptions = prescriptionService.findAll();
        return ResponseEntity.ok(prescriptions);
    }
    
    /**
     * 根据处方ID获取处方信息
     * 
     * @param id 处方ID
     * @return 处方信息，如果未找到返回404
     */
    @GetMapping("/{id}")
    public ResponseEntity<Prescription> getPrescriptionById(@PathVariable Long id) {
        Prescription prescription = prescriptionService.findById(id);
        if (prescription != null) {
            return ResponseEntity.ok(prescription);
        }
        return ResponseEntity.notFound().build();
    }
    
    /**
     * 根据处方号获取处方信息
     * 
     * @param number 处方号
     * @return 处方信息，如果未找到返回404
     */
    @GetMapping("/number/{number}")
    public ResponseEntity<Prescription> getPrescriptionByNumber(@PathVariable String number) {
        Prescription prescription = prescriptionService.findByNumber(number);
        if (prescription != null) {
            return ResponseEntity.ok(prescription);
        }
        return ResponseEntity.notFound().build();
    }
    
    /**
     * 根据患者ID获取该患者的所有处方
     * 按处方日期倒序排列
     * 
     * @param patientId 患者ID
     * @return 该患者的处方列表
     */
    @GetMapping("/patient/{patientId}")
    public ResponseEntity<List<Prescription>> getPrescriptionsByPatientId(@PathVariable Long patientId) {
        List<Prescription> prescriptions = prescriptionService.findByPatientId(patientId);
        return ResponseEntity.ok(prescriptions);
    }
    
    /**
     * 根据医师姓名获取该医师开具的所有处方
     * 按处方日期倒序排列
     * 
     * @param doctorName 医师姓名
     * @return 该医师的处方列表
     */
    @GetMapping("/doctor/{doctorName}")
    public ResponseEntity<List<Prescription>> getPrescriptionsByDoctorName(@PathVariable String doctorName) {
        List<Prescription> prescriptions = prescriptionService.findByDoctorName(doctorName);
        return ResponseEntity.ok(prescriptions);
    }
    
    /**
     * 根据科室获取该科室的所有处方
     * 按处方日期倒序排列
     * 
     * @param department 科室名称
     * @return 该科室的处方列表
     */
    @GetMapping("/department/{department}")
    public ResponseEntity<List<Prescription>> getPrescriptionsByDepartment(@PathVariable String department) {
        List<Prescription> prescriptions = prescriptionService.findByDepartment(department);
        return ResponseEntity.ok(prescriptions);
    }
    
    /**
     * 根据日期范围查询处方
     * 
     * @param startDate 开始日期时间
     * @param endDate 结束日期时间
     * @return 指定日期范围内的处方列表
     */
    @GetMapping("/date-range")
    public ResponseEntity<List<Prescription>> getPrescriptionsByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        List<Prescription> prescriptions = prescriptionService.findByDateRange(startDate, endDate);
        return ResponseEntity.ok(prescriptions);
    }
    
    /**
     * 获取处方总数量
     * 
     * @return 处方总数量
     */
    @GetMapping("/count")
    public ResponseEntity<Integer> getPrescriptionCount() {
        int count = prescriptionService.count();
        return ResponseEntity.ok(count);
    }
}